package com.mbuswidget;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MbusWidgetConfigurationSettingsBlacklisted extends Activity {
	private int widgetID;
	private ListView itemList;
	private ArrayAdapter<String> itemAdapter;
	ArrayList<String> blacklistedRoutes;
	SharedPreferences prefs;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blacklistedroutes);

		widgetID = getIntent().getExtras().getInt(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);

		if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID)
			finish();

		prefs = PreferenceManager.getDefaultSharedPreferences(this);

		blacklistedRoutes = MBusWidgetConstants.GetWidgetBlacklist(prefs,
				widgetID);
		itemList = (ListView) findViewById(R.id.routeList);
		itemAdapter = new ArrayAdapter<String>(
				MbusWidgetConfigurationSettingsBlacklisted.this,
				android.R.layout.simple_list_item_1, blacklistedRoutes);
		itemList.setAdapter(itemAdapter);
		itemList.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {

			}
		});

		Button addRoute = (Button) findViewById(R.id.addRouteBtn);
		addRoute.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(
						MbusWidgetConfigurationSettingsBlacklisted.this,
						MbusWidgetConfigurationSettingsBlacklistedAddRoute.class);
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				startActivityForResult(intent, 0);
			}
		});
		
		Button removeRoute = (Button) findViewById(R.id.removeRouteBtn);
		removeRoute.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(
						MbusWidgetConfigurationSettingsBlacklisted.this,
						MbusWidgetConfigurationSettingsBlacklistedRemoveRoute.class);
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				startActivityForResult(intent, 0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case (0): {
			if (resultCode == Activity.RESULT_OK) {
				// We have information required. Complete Widget setup.

				String[] routesToAdd = data
						.getStringArrayExtra(MBusWidgetConstants.BLACKLISTED_ROUTE_ADD);
				if (routesToAdd != null) {
					Collections.addAll(blacklistedRoutes, routesToAdd);
					Collections.sort(blacklistedRoutes);
					MBusWidgetConstants.SetWidgetBlacklist(prefs, widgetID,
							blacklistedRoutes);
					itemAdapter.notifyDataSetChanged();
				}
				
				String[] routesToRemove = data
						.getStringArrayExtra(MBusWidgetConstants.BLACKLISTED_ROUTE_REMOVE);
				if (routesToRemove != null) {
					for(int x = 0; x < routesToRemove.length; x++) {
						blacklistedRoutes.remove(routesToRemove[x]);
					}
					Collections.sort(blacklistedRoutes);
					MBusWidgetConstants.SetWidgetBlacklist(prefs, widgetID,
							blacklistedRoutes);
					itemAdapter.notifyDataSetChanged();
				}

			}
			break;
		}
		}
	}
}
